<template>
  <div id="cluereport">
    <el-dialog
      title="查看纪检机构信息"
      :visible.sync="unitInfoVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <unitFormShow :projectId="projectId" :timestamp="timestamp" :readOnly="readonly" @saveUnitData="saveUnitData" :unitData="unitData" :project="project" />
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/unitReport' }">统计填报</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/unitReport/unitList/'+ project.id}">{{project.title}}</el-breadcrumb-item>
      <el-breadcrumb-item>查看</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <h1>单位名称：{{unitname}}<span v-if="errorText" style="color:red">（{{errorText}}）</span></h1>
      <div style="line-height:200%;margin:10px;">
        填报起止日期：{{project.beginDate}} 至 {{project.endDate}}<br>{{project.remark}}
      </div>
    </section>
    <section class="unitbox" style="margin-top:10px">
    <!-- <section style="margin-top:10px"> -->
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="tableHeight" border
        ref="multipleTable"
        >
        <el-table-column type="index" label="序号" width="60" align="center" class-name="tableth">
        </el-table-column>
        <el-table-column label="承担纪检工作机构名称" min-width="200" prop="deptName" />
        <el-table-column label="行政编制" width="110" prop="unitOrg1" align="center" />
        <el-table-column label="事业编制" width="110" prop="unitOrg2" align="center" />
        <el-table-column label="企业编制" width="110" prop="unitOrg3" align="center" />
        <el-table-column label="编制总数" prop="allOrg" width="110" align="center">
          <template slot-scope="scope">
            {{(scope.row.unitOrg1?scope.row.unitOrg1:0)+(scope.row.unitOrg2?scope.row.unitOrg2:0)+(scope.row.unitOrg3?scope.row.unitOrg3:0)}}
          </template>
        </el-table-column>
        <el-table-column label="人员交流人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{(scope.row.num1_1?scope.row.num1_1:0)+(scope.row.num1_2?scope.row.num1_2:0)}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员严重疾病人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{scope.row.num2_1?scope.row.num2_1:0}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员伤残人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{scope.row.num3?scope.row.num3:0}}
          </template>
        </el-table-column>
        <el-table-column label="在岗人员死亡人数" prop="allOrg" width="110" align="center" v-if="project.projectVersion&&project.projectVersion==='v2'">
          <template slot-scope="scope">
            {{(scope.row.num4_1?scope.row.num4_1:0)+(scope.row.num4_2?scope.row.num4_2:0)+(scope.row.num4_3?scope.row.num4_3:0)+(scope.row.num4_4?scope.row.num4_4:0)}}
          </template>
        </el-table-column>
        <el-table-column label="机构奖励数量" width="110" prop="awards" align="center">
          <template slot-scope="scope">
            {{scope.row.awards?scope.row.awards.length:0}}
          </template>
        </el-table-column>
        <el-table-column label="实有人数" width="110" prop="personNum" align="center" />
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="primary" size="mini" style="margin:10px 0 10px 5px">机构信息</el-button>
            <el-button @click="detail(scope.row.id)" type="success" size="mini" style="margin:10px 0 10px 5px">人员信息</el-button>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
import unitFormShow from './customUnitFormShow'
export default {
  components: { unitFormShow
  },
  data () {
    return {
      errorText: '',
      unitData: {},
      unitInfoVisible: false,
      total: 0,
      pageNum: 1,
      pageSize: 20,
      totalnum: 0,
      Isace: true,
      Sortkey: '',
      formShowVisible: false,
      timestamp: '',
      projectId: null,
      dialogTitle: '查看',
      items: [],
      unitname: '',
      project: {},
      listLoading: true,
      dataLoading: true,
      tableHeight: 100,
      list: [
      ],
      changeSizeFlag: false,
      readonly: true,
      item: {},
      fileIds: []
    }
  },
  methods: {
    saveUnitData () {
    },
    addData (item) {
      this.formShowVisible = false
      this.fileIds = []
      Object.keys(item).forEach(key => {
        JSON.parse(this.project.reportTemplate).columns.find(tempitem => { // 从模板中找出模板项为该key的配置项
          if (tempitem.title === key && tempitem.type === 'upload') { // 并且该配置项为文件类型
            if (item[key].length > 0) {
              this.fileIds = [...this.fileIds, ...item[key].map(files => files.id)]
            }
          }
        })
      })
      let para = {
        dataJson: item,
        fileIds: this.fileIds,
        projectDataId: parseInt(this.$route.params.id)
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/saveDataItem`, para)
        .then(response => {
          if (response.data.success) {
            this.fileIds = []
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络保存失败:' + error,
            type: 'error'
          })
        })
    },
    saveData (state) {
      this.list.forEach(item => { // 遍历填写的数据记录
        Object.keys(item).forEach(key => {
          JSON.parse(this.project.reportTemplate).columns.find(tempitem => { // 从模板中找出模板项为该key的配置项
            if (tempitem.title === key && tempitem.type === 'upload') { // 并且该配置项为文件类型
              if (item[key].length > 0) {
                this.fileIds = [...this.fileIds, ...item[key].map(files => files.id)]
              }
            }
          })
        })
      })
      let para = {
        dataJson: JSON.stringify(this.list),
        projectDataId: parseInt(this.$route.params.id),
        state: state,
        fileIds: this.fileIds
      }
      console.log(para)
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/saveProjectData`, para)
        .then(response => {
          if (response.data.success) {
            this.fileIds = []
            if (state === 1) {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$router.push('/unit/unitReport')
            }
          } else {
            this.$message({
              message: '提交失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络提交失败:' + error,
            type: 'error'
          })
        })
    },
    edit (item) {
      this.timestamp = new Date().getTime()
      this.unitInfoVisible = true
      this.unitData = item
    },
    getProjectDetail (id) {
      this.dataLoading = true
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectById?id=` + id)
        .then(response => {
          this.project = response.data.result
          this.dataLoading = false
          this.listLoading = false
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
          this.dataLoading = false
          this.listLoading = false
        })
    },
    getPersonNum (id, index) {
      let para = {
        projectDataId: this.$route.params.id,
        pageNum: 1,
        pageSize: 1,
        deptId: id
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/queryDataItemByPage`, para)
        .then(response => {
          this.$set(this.list[index], 'personNum', response.data.result.total)
        })
        .catch(error => {
          console.log(error)
          return 0
        })
    },
    getProjectDataDetail (id) {
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectDataById?id=` + id)
        .then(response => {
          this.unitname = response.data.result.unitName
          this.projectId = response.data.result.projectId
          this.getProjectDetail(this.projectId)
          if (response.data.result.dataJson) {
            this.list = JSON.parse(response.data.result.dataJson)
            this.list.forEach((item, index) => {
              this.getPersonNum(item.id, index)
            })

            let errorNum = 0
            let errortext1 = ''
            this.list.forEach((item, index) => {
              let errorFlag = false
              if (!item.num1_1 && !item.num1_2 && item.content1) {
                errortext1 = '机构“' + item.deptName + '”人员交流情况描述不为空，但人员交流人数为0'
                errorFlag = true
              }
              if (!item.num2_1 && !item.num2_2 && item.content2) {
                errortext1 = '机构“' + item.deptName + '”在岗人员严重疾病情况描述不为空，但在岗人员严重疾病人数为0'
                errorFlag = true
              }
              if (!item.num3 && item.content3) {
                errortext1 = '机构“' + item.deptName + '”在岗人员伤残情况描述不为空，但在岗人员伤残人数为0'
                errorFlag = true
              }
              if (!item.num4_1 && !item.num4_2 && !item.num4_3 && !item.num4_4 && item.content4) {
                errortext1 = '机构“' + item.deptName + '”在岗人员死亡情况描述不为空，但在岗人员死亡人数为0'
                errorFlag = true
              }
              if (errorFlag) { errorNum++ }
            })
            if (errorNum > 0) {
              this.errorText = errorNum + '家纪检机构填写有误，如：' + errortext1
            }
          } else {
            this.list = []
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    detail (id) {
      this.$router.push('/unitReport/customUnitProjectDeptDetail/' + this.$route.params.id + ',' + id)
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 243

      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.getProjectDataDetail(this.$route.params.id)
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
  }
}
</script>
<style>
#cluereport .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .unitbox .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .unitbox .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #cluereport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
</style>
<style scoped>
  #cluereport .el-button{
    color:#fff !important;
  }
  #cluereport h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #cluereport .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .unitbox{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
